package com.diuse.outer.analyse.tongcheng;

import com.diuse.outer.executor.HttpHelper;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * http://hz.58.com/yuhang/zufang/j3/?minprice=1000_2000
 *
 * yuhang表示具体的位置
 * j3表示几个人间：如果不加表示没有限制  j1-j5分别表示1-5人间
 * minprice表示价格区间
 * Created by Administrator on 2015/11/20 0020.
 */
public class SummaryClient {

    private List<String> urlList = new ArrayList<String>(); //  解析出来的一级url链接
    private String loc;     //要租房的位置，输入为中文拼音
    private int roomCnt;    //房间的个数，0为不限制
    private int minPrice;   //房租的底价
    private int maxPrice;   //房租的顶价
    private String url;     //拼凑出来的url

    private static final String locPlaceHolder = "#Loc#";
    private static final String roomSizePlaceHolder = "#ROOM_SIZE#";
    private static final String minPricePlaceHolder = "#MIN_PRIVE#";
    private static final String maxPricePlaceHolder = "#MAX_PRICE#";

    private static final String urlTemplate = "http://hz.58.com/"+ locPlaceHolder + "/zufang/" + roomSizePlaceHolder +
                                                "/?minprice=" + minPricePlaceHolder + "_"+maxPricePlaceHolder;


    public SummaryClient(String location, int roomCnt, int minPrice, int maxPrice) {
        this.loc = location;
        this.roomCnt = roomCnt;
        this.minPrice = minPrice;
        this.maxPrice = maxPrice;
        initalUrl();
    }

    public SummaryClient(String location, int minPrice, int maxPrice) {
        this(location,0,minPrice,maxPrice);
    }

    private void initalUrl() {
        url = urlTemplate.replaceFirst(locPlaceHolder, loc)
                .replaceFirst(minPricePlaceHolder, String.valueOf(minPrice))
                .replaceFirst(maxPricePlaceHolder, String.valueOf(maxPrice));
        if (roomCnt == 0) {
            url = url.replaceFirst(roomSizePlaceHolder + "/" ,"");
        } else {
            url = url.replaceFirst(roomSizePlaceHolder, "j" + String.valueOf(roomCnt));
        }

    }

    public static void main(String[] argus) throws Exception {
        String result = HttpHelper.requestGet("http://hz.58.com/gudang/zufang/?PGTID=166360291189792991989550094&ClickID=1");
        System.out.println(result);
    }

    public void run() throws Exception {

    }

//    public
}
